package answer

import "sort"

func findUnsortedSubarray(nums []int) int {
	//1.临时数组先排序
	tmpNums := make([]int, 0)
	tmpNums = append(tmpNums, nums...)
	sort.Ints(tmpNums)
	//2.找到数组中数字不相等的两个下标
	begin, end := -1, -1
	for i := 0; i < len(nums); i++ {
		if nums[i] != tmpNums[i] {
			if begin == -1 {
				begin = i
			}
			end = i
		}
	}
	if end == begin {
		return 0
	} else {
		return end - begin + 1
	}
}
